/*
* 弹窗插件
* create by shlong 2017年2月15日
* 调用样例
* var confirmConfig = {
 nav:[{id:1,name:'添加评审专家',def:true},{id:2,name:'测试标签',def:false}],
 content:[
 {id:1,def:true,type:'form',formId:'addExpert',buttonId:'addExpertButton',data: [
 {type:'text',name:'name',title:'姓名',placeholder:'请输入姓名'},
 {type:'text',name:'loginname',title:'登录名',placeholder:'请输入登录名'},
 {type:'text',name:'loginpassword',title:'登录密码',placeholder:'请输入登录密码'},
 {type:'radio',name:'sex',title:'性别',option:
 [{id:1,name:'男',value:'1',def:true},
 {id:2,name:'女',value:'2',def:false}
 ]
 },
 {type:'text',name:'position',title:'职位',placeholder:'请输入职位'},
 {type:'text',name:'mobile',title:'电话',placeholder:'请输入电话'},
 {type:'text',name:'email',title:'Email',placeholder:'请输入Email'},
 {type:'textarea',name:'info',title:'简介',placeholder:'请输入简介'}

 ],buttonEven:function (formData) {//按钮点击事件
 return addExpertAjax(formData)
 }},
 {id:2,def:false,type:'html',html:'66666' }

 ]

 };
 $.SMsgBox.Confirm(confirmConfig,function () {});
* */


(function () {
    $.SMsgBox = {
        Confirm:function (data ,callback) {
            console.log(data);
            confirmHtml(data);
            _bindEven(data);
            _closeConfire();
            _tabNav();
            _manyTextDel();
            _manyTextAdd();
            _uploadPic(data._csrf);
            niceScrollresize();
            $(".niceScroll").niceScroll({
                touchbehavior:false,
                cursorcolor:"#666",
                cursoropacitymax:1,
                cursorwidth:5,
                cursorborder:"none",
                cursorborderradius:"4px",
                background:"#999",
                //autohidemode:false
            });
            callback();
        }
    };
    var confirmHtml = function (data) {
        var _html = "";
        _html += '<div id="confirm" class="mes_box_body mes_news_box" style="display:block;">' +
            '<div class="headerw">' +
            '<div class="header">' +
            '<span class="del"></span>' ;
        $.each(data.nav,function (i,v) {

            _html += '<span class="heading';
            if(v.def){
                _html += ' on';
            }
            _html += '" for="confirmBody_'+v.id+'">'+v.name+'</span>';
            if(data.nav.length > i+1){
                _html += '&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;';
            }
        });
        _html+= '<br clear="all"/>' +
            '</div>' +
            '</div>';
             $.each(data.content,function (key,va) {
                 //表单页面
                 if(va.type == 'form'){
                     //表单内容

                     _html += '<div class="ipt_body content niceScroll " id="confirmBody_'+va.id+'" ';
                     if(va.def){
                         _html += ' style="overflow: auto;height: 400px;display:block"';
                     }else{
                         _html += ' style="overflow: auto;height: 400px;display:none"';
                     }
                     _html += '><form id="'+va.formId+'">';

                         //内容
                         $.each(va.data,function (i,v) {
                             if(v.type == 'text' ||v.type == 'TextField'){
                                 //
                                 _html += '<div >' +
                                     '<span class="text">'+v.title+'</span>' +
                                     '<input type="text" name="'+v.name+'" placeholder="'+v.placeholder+'" value="'+v.value+'"/>' +
                                     '</div>';
                             }
                             if(v.type == 'textarea' || v.type == 'TextArea'){
                                 _html += ' <div>' +
                                     '<span class="text">'+v.title+'</span>' +
                                     '<textarea name="'+v.name+'" placeholder="'+v.placeholder+'">'+v.value+'</textarea>' +
                                     '</div>';
                             }
                             if(v.type == 'manyText'){
                                _html += '<div class="numIpt2">' +
                                    '<span class="text">'+v.title+'</span>';

                                    if(v.value){
                                        $.each(v.value,function (c,d) {
                                            _html +='<div class="iptBox1">' ;
                                            $.each(v.option,function (a,b) {
                                                var width = '';
                                                if(b.width){
                                                     width = 'style="width:'+b.width+'"';
                                                }
                                                _html +='<input value="'+v.value[c][b.name]+'" '+width+'   class="'+b.class+'" name="'+v.name+'['+b.name+'][]" type="text" placeholder="'+b.placeholder+'"/>';
                                            });
                                            _html += '<input type="hidden" name="'+v.name+'[id][]" value="'+v.value[c]["id"]+'">';
                                            _html += '<span class="clos_btn" style=" margin-right: 30px;"></span></div>';
                                        });
                                    }else{

                                        _html +='<div class="iptBox1">' ;
                                        $.each(v.option,function (a,b) {
                                            var width = '';
                                            if(b.width){
                                                width = 'style="width:'+b.width+'"';
                                            }
                                            _html +='<input class="'+b.class+'"  '+width+' name="'+v.name+'['+b.name+'][]" type="text" placeholder="'+b.placeholder+'"/>';
                                        });
                                        _html += '<span class="clos_btn" style=" margin-right: 30px;"></span></div>';

                                    }
                                    var optionStr = JSON.stringify(v.option);
                                     _html +=  '<a class="add_itemBtn" i = "'+v.name+'" data=\''+optionStr+'\'></a>'+
                                     '</div>';
                             }
                             if(v.type == 'radio' || v.type == 'Radio' || v.type == 'Select'){
                                 _html += '<div class="ipt_text3">' +
                                     '<span class="text">'+v.title+'</span>' +
                                     '<div class="fromGroup">';

                                 $.each(v.option,function (a,b) {
                                     var checked = '';
                                     if(v.value){
                                         if(v.value == b.value){
                                             checked = 'checked';
                                         }
                                     }else{
                                         if(b.def){
                                             checked = 'checked';
                                         }
                                     }

                                     _html += '<input type="radio" class="radio" name="'+v.name+'" value="'+b.value+'" id="radio_'+b.id+'" '+checked+'/>' +
                                         '<label for="radio_'+b.id+'">'+b.name+'</label>' ;
                                 });
                                     _html += '</div><br clear="all"/>' +
                                     '</div>';
                             }
                             if(v.type == 'hidden'){
                                 //
                                 _html +=  '<input type="hidden" name="'+v.name+'" value="'+v.value+'"/>' ;
                             }

                             if(v.type == 'UploadPic'){
                                 _html += ' <div class="ipt_text9">' +
                                     '<span class="text">'+v.title+'</span>' +
                                     '<ul class="imgList">' +
                                     '<li><img src="/static/Static/img/mb/topPic.jpg" width="120" /><i class="del"></i></li>' +
                                     '<li><img src="/static/Static/img/mb/topPic.jpg" width="120" /><i class="del"></i></li>' +
                                     '<li><img src="/static/Static/img/mb/topPic.jpg" width="120" /><i class="del"></i></li>' +
                                     '<li><img src="/static/Static/img/mb/topPic.jpg" width="120" /><i class="del"></i></li>' +
                                     '<br clear="all"/>' +
                                     '</ul>' +
                                     '<label for="imgfile" class="imgfile_lab" id="PicUpload_9" name="'+v.name+'">' +
                                     '</label>' +
                                     '</div>';

                             }
                             if(v.type == 'UploadFile'){
                                  _html += '<div class="ipt_text10">' +
                                      '<span class="text">'+v.title+'</span>' +
                                      '<label for="docfile" class="docfile_lab">上传文件</label>' +
                                      '<input type="file" id="docfile" name="imgfile" />' +
                                      '<ul class="docList">' +
                                      '<li>' +
                                      '<span class="listT">只会社区文字.doc(35.56KB)</span>' +
                                      '<i class="del"></i>' +
                                      '</li>' +
                                      '</ul>' +
                                      '</div>';
                             }
                             if(v.type == 'CheckBox'){
                                 _html += '<div class="ipt_text8">' +
                                     '<span class="text">'+v.title+'</span>' +
                                     '<div class="fromGroup">';
                                 if(v.value){
                                     var valueObj = v.value.split(",");
                                 }
                                 console.log('---------------');
                                 console.log(valueObj);
                                 $.each(v.option,function (a,b) {
                                     var checked = '';
                                     if($.inArray(b.value,valueObj) != -1){
                                         checked = 'checked';
                                     }
                                     _html += ' <input type="checkbox" class="checkbox" name="'+v.name+'[]" value="'+b.value+'" id="checkbox_'+b.id+'" '+checked+'/>' +
                                         '&nbsp;&nbsp;<label for="checkbox_'+b.id+'">'+b.name+'</label>&nbsp;&nbsp;&nbsp;&nbsp;' ;
                                 });
                                 _html += '</div><br clear="all"/>' +
                                     '</div>';
                             }


                         });

                         _html+='<div class="ipt_text8">' +
                         '<input type="button" id="'+va.buttonId+'" value="提交"/></form>' +
                         '</div>' +
                         '</div>';
                 }

                 //html页面
                 if(va.type == 'html'){
                     _html += '<div class="content '+va.classstr+'" id="confirmBody_'+va.id+'"' ;
                     if(va.def){
                         _html += ' style="width: 540px; margin: 14px auto 35px;display:block"';
                     }else{
                         _html += ' style="width: 540px; margin: 14px auto 35px;display:none"';
                     }
                         _html+='>'+va.html+'</div>';
                 }

             });
            //type : form
            _html += '</div>' +
            '<div class="body_bg" style="display:block;"></div>';
            $("body").append(_html);
    };
    //绑定函数事件
    var _bindEven = function (data) {
        $.each(data.content,function (i,v){
                if(v.type == 'form'){
                    $('#'+v.buttonId).on('click',function () {
                            var formData = $('form[id='+v.formId+']').serialize();
                            v.buttonEven(formData);
                    });
                }
                if(v.type == 'html'){
                    $('#'+v.buttonId).on('click',function () {
                        v.buttonEven();
                    });
                }
        });
    };
    //关闭弹窗
    var _closeConfire = function () {
        $('.del').on('click',function () {
            niceScrollresize();
             $('#confirm').remove();
             $('.body_bg').remove();

        });
        $('.body_bg').on('click',function () {
            niceScrollresize();
            $('#confirm').remove();
            $('.body_bg').remove();

        });
    };
    //tab切换
    var _tabNav = function(){
        $(".mes_news_box").find(".heading").click(function () {
            $(this).addClass("on").siblings().removeClass("on");
            var id = $(this).attr("for");
            $("#"+id).show().siblings().hide();
            $(this).parents().show();

        });
    };
    //多行、删除行动作
    var _manyTextDel = function () {
        niceScrollresize();
        $(document).on('click','.clos_btn',function () {
            $(this).parents('.iptBox1').remove();
        });
    };

    //多行、新增行动作
    var _manyTextAdd = function () {
        niceScrollresize();
        $('.add_itemBtn').on('click',function () {
            var dataStr = $(this).attr('data');
            var dataObj = $.parseJSON(dataStr);
            var a = $(this).attr('i');
            _inputHtml = '';
            _inputHtml +='<div class="iptBox1">' ;
            $.each(dataObj,function (i,v) {
                var width = '';
                if(v.width){
                    width = 'style="width:'+v.width+'"';
                }
                _inputHtml +='<input class="'+v.class+'" '+width+' name="'+a+'['+v.name+'][]" type="text" placeholder="'+v.placeholder+'"/>';
            });
            _inputHtml += '<span class="clos_btn" style=" margin-right: 30px;"></span></div>';

            $(this).before(_inputHtml);

        });
    };

    var _uploadPic = function (_csrf) {
        $('[id^="PicUpload_"]').uploadifive({
            'auto'             : true,
            'buttonClass'      : 'imgfile_lab',
            'removeCompleted' : true,
            'fileSizeLimit' : 20000,
            'formData':{
                '_csrf':_csrf
            },
            'uploadScript'     : '/review/main/uploadpic',
            'onUploadComplete' : function(file, data) {
                //console.log(data);
                var dataobj = jQuery.parseJSON(data);
                if(dataobj.status == 1){
                    var name = $(this).attr('name');
                    $(this).parent().siblings('ul').append('<li><input type="hidden" name="'+name+'" value="'+dataobj.path+'"><img src="'+dataobj.imgurl+'" width="120" height="120" /><i class="del"></i></li>');
                    //console.log($(this));
                }else{
                    BC.showError('不合法类型');
                }
            }
        });
    }

})();